package com.googlecode.moneyManager.service;

import com.googlecode.moneyManager.dao.SecurityValueDAO;
import com.googlecode.moneyManager.exceptions.ServiceLayerException;
import com.googlecode.moneyManager.model.Amount;
import com.googlecode.moneyManager.model.Currency;
import com.googlecode.moneyManager.model.SecurityValue;
import com.googlecode.moneyManager.model.TypeSecurityValue;
import com.googlecode.moneyManager.model.dto.InputSecurityDTO;
import com.googlecode.moneyManager.model.dto.SecurityValueDTO;
import com.googlecode.moneyManager.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Author: Sergiy Doroshenko
 * Date: Apr 28, 2010
 * Time: 12:17:29 PM
 */
public class SecurityValueServiceImpl implements SecurityValueService{

    private SecurityValueDAO securityValueDAO;
    

    public void addValue(String value,TypeSecurityValue type, Currency currency) throws ServiceLayerException{
//        Double valueDouble = 0.0;
//        try{
//            valueDouble = Double.parseDouble(value);
//        }catch (NumberFormatException e){
//            throw new ServiceLayerException(" wrong input grivnas value ",e);
//        }
        //select s.date,s.type,a.amount,a.currency from SecurityValue s left join amount a on a.id=s.amount_id;
        Assert.notNull(value, " argument value is null !!!");
        //@TODO use Joda Time library joda-time.sourceforge.net instead of Calendar and date
        Calendar now = Calendar.getInstance();
        SecurityValue securityValue = securityValueDAO.getSecurityValueByDate(now,type,currency);
        if(securityValue==null){
            securityValue = new SecurityValue();
            securityValue.setDate(now);
            securityValue.setAmount(new Amount(value, currency));
            securityValue.setType(type);
            String id = securityValueDAO.create(securityValue);
            System.out.println("ID = " + id);
        }else{
            Amount newAmount = new Amount(value, currency);
            if(!securityValue.getAmount().getAmount().equals(newAmount.getAmount())){
                securityValue.setAmount(newAmount);
                securityValueDAO.saveOrUpdate(securityValue);
            }
        }


    }

    @Override
    public SecurityValue getSecurityValueByDate(Calendar date, TypeSecurityValue type, Currency currency) {

        return securityValueDAO.getSecurityValueByDate(date,type,currency);
    }

    @Override
    public void addValues(InputSecurityDTO inputSecurityDTO) {
        addValue(inputSecurityDTO.baksAll, TypeSecurityValue.ALL, Currency.USD);
        addValue(inputSecurityDTO.grivnaAll, TypeSecurityValue.ALL,Currency.UAH);
        addValue(inputSecurityDTO.baksPart, TypeSecurityValue.PART, Currency.USD);
        addValue(inputSecurityDTO.evroPart, TypeSecurityValue.PART, Currency.EUR);
        addValue(inputSecurityDTO.grivnaPart,TypeSecurityValue.PART,Currency.UAH);
        addValue(inputSecurityDTO.baksRate,TypeSecurityValue.RATE,Currency.USD);
        addValue(inputSecurityDTO.evroRate, TypeSecurityValue.RATE, Currency.EUR);
    }

    @Override
    public List<SecurityValue> getValuesByType(TypeSecurityValue type) {
        return null;
    }

    @Override
    public List<SecurityValueDTO> getAllValues() {
        List<SecurityValue> values = securityValueDAO.getAll();
        List<SecurityValueDTO> dtos = new ArrayList<SecurityValueDTO>();
        for (SecurityValue value : values){
            SecurityValueDTO dto = new SecurityValueDTO();
            dto.amount = value.getAmount().getAmount().doubleValue();
            dto.date = value.getDate().getTime();
        }
        return dtos;
    }

    public SecurityValueDAO getSecurityValueDAO() {
        return securityValueDAO;
    }

    public void setSecurityValueDAO(SecurityValueDAO securityValueDAO) {
        this.securityValueDAO = securityValueDAO;
    }
}
